package com.skycode7.servlet;

import com.skycode7.bean.NoticeBean;
import com.skycode7.dao.NoticeDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/NoticeEdit.jsp")
public class NoticeEditServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String NoticeId = req.getParameter("NoticeId");
        String NoticeName= req.getParameter("NoticeName");
        String NoticeContent = req.getParameter("NoticeContent");
        String NoticeTime = req.getParameter("NoticeTime");
        String NoticeTitle = req.getParameter("NoticeTitle");

        NoticeBean noticeBean= new NoticeBean();//实例化一个新闻对象
        //设置新闻对象属性值
        noticeBean.setNoticeID(Integer.parseInt(NoticeId));
        noticeBean.setNoticeTitle(NoticeTitle);
        noticeBean.setAdminName(NoticeName);
        noticeBean.setNoticeContent(NoticeContent);
        noticeBean.setNoticeTime(NoticeTime);
        NoticeDao noticeDao = new NoticeDao();
        int result = noticeDao.add(noticeBean);
        if (result > 0) {
            //补充知识 跳转到网页news.html中
            resp.sendRedirect("news.html");//使用结果对象 完成页面网址跳转
        } else {//失败显示错误提示页面
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req, resp);
        }
    }
}